<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<%
			response.addHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
		%>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>系统</title>

		<script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" src="js/vtip-min.js"></script>
		<script type="text/javascript" src="jeasyui/jquery.easyui.min.js"></script>
		<script type="text/javascript"
			src="jeasyui/locale/easyui-lang-zh_CN.js"></script>
		<script type="text/javascript" src="js/common.js"></script>
		<link rel="stylesheet" type="text/css"
			href="jeasyui/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="jeasyui/themes/icon.css">
		<link rel="stylesheet" type="text/css" href="css/sys.css">
		<script>

	$(function() {
		$("#moduleTree").tree({
			url : "module.do?method=showModuleTree",
			onClick : function() {
				var node = $("#moduleTree").tree("getSelected");
				showModuleToRoleTab(node.id);
			},
			onLoadError:function(){
				$.messager.alert("提示", "模块树加载数据出错~~", "error");
			}
		});
	});
	//模块树刷新方法
	function reload() {
		var node = $("#moduleTree").tree("getSelected");
		if (node) {
			$("#moduleTree").tree("reload", node.target);
		} else {
			$("#moduleTree").tree("reload");
		}
	}
	//显示模块对应的角色列表
	function showModuleToRoleTab(nodeid) {
		$("#moduletab").datagrid({
			title : "模块对应的角色",
			fit : true,
			pageSize : 20,
			pageList : [ 20, 30, 50 ],
			url : "module.do?method=getRolesBymoduleId&moduleId=" + nodeid,
			columns : [ [ {
				field : "ck",
				checkbox : true
			}, {
				field : "roleName",
				title : "模块对应角色名称",
				width : $(this).width() * 0.3,
				formatter : function(value, rec) {
					return rec.sysRole.roleName;
				}
			}, {
				field : "remark",
				title : "角色说明",
				width : $(this).width() * 0.3,
				formatter : function(value, rec) {
					return rec.sysRole.remark;
				}
			} ] ],
			pagination : true,
			toolbar : [ {
				text : "添加所选模块对应角色",
				iconCls : "icon-add",
				handler : function() {
					addroleToModule();
				}
			}, "-", {
				text : "删除所选模块对应角色",
				iconCls : "icon-remove",
				handler : function() {
					delteRoleFromModule();
				}
			} ]
		});
	}
	//添加角色到对应模块
	function addroleToModule() {
		var node = $("#moduleTree").tree("getSelected");
		$("#roletomodule").datagrid({
			pageSize : 10,
			pageList : [ 10, 20, 50 ],
			url : "role.do?method=getList",
			columns : [ [ {
				field : "ck",
				checkbox : true
			}, {
				field : "roleName",
				title : "角色名称",
				width : $(this).width() * 0.25
			}, {
				field : "remark",
				title : "角色说明",
				width : $(this).width() * 0.25
			} ] ],
			pagination : true
		});
		$("#selectrole").show();
		$("#selectrole").dialog({
			title : "选择角色",
			modal : true,
			width : 580,
			height : 400,
			collapsible : true,
			minimizable : false,
			maximizable : false,
			buttons : [ {
				text : "提交",
				iconCls : "icon-ok",
				handler : function() {
					var ids = [];
					var rows = $("#roletomodule").datagrid("getSelections");
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].roleId);
					}
					if (ids.length > 0) {
						$.ajax({
							type : "POST",
							dataType : "text",
							url : "module.do?method=addRoleToModule&nowdate=" + new Date(),
							data : {
								"roleIds" : ids.join(),
								"moduleId" : node.id
							},
							success : function(data) {
								$.messager.show({
									title : "提示信息",
									msg : data,
									showType : "show",
									timeout : 3000
								});
								showModuleToRoleTab(node.id);
							}
						});
						$("#selectrole").dialog("close");
					} else {
						$.messager.alert("提示信息", "没有选择角色！", "info");
					}

				}
			}, {
				text : "取消",
				handler : function() {
					$("#selectrole").dialog("close");
				}
			} ]
		});
	}
	//删除模块对应的角色
	function delteRoleFromModule() {
		var node = $("#moduleTree").tree("getSelected");
		var ids = [];
		var rows = $("#moduletab").datagrid("getSelections");
		for ( var i = 0; i < rows.length; i++) {
			ids.push(rows[i].moduleRoleId);
		}
		if (ids.length > 0) {
			$.ajax({
				type : "POST",
				dataType : "text",
				url : "module.do?method=deleteRoleFromModule&nowdate=" + new Date(),
				data : {
					"ids" : ids.join()
				},
				success : function(data) {
					$.messager.show({
						title : "提示信息",
						msg : data,
						showType : "show",
						timeout : 3000
					});
					showModuleToRoleTab(node.id);
				}
			});
		} else {
			$.messager.alert("提示信息", "没有选择要删除的角色", "info");
		}
	}
	//添加模块
	function addmodule() {
		var node = $("#moduleTree").tree("getSelected");
		if (node == null) {
			$("#modulePid").attr("value", "-1");
		} else {
			$("#modulePid").attr("value", node.id);
		}
		$("#addmodule").show();
		$("#addmodule").dialog({
			title : "添加模块",
			modal : true,
			width : 280,
			height : 200,
			collapsible : true,
			minimizable : false,
			maximizable : false,
			buttons : [ {
				text : "提交",
				iconCls : "icon-ok",
				handler : function() {
					$("#moduleform").form("submit", {
						url : "module.do?method=addModule",
						onSubmit : function() {
							return $(this).form("validate");
						},
						success : function(data) {
							$("#addmodule").dialog("close");
							$.messager.show({
								title : "提示信息",
								msg : data,
								showType : "show",
								timeout : 3000
							});
							reload();
						}
					});
				}
			}, {
				text : "取消",
				handler : function() {
					$("#addmodule").dialog("close");
				}
			} ]
		});
	}
	//更新模块
	function updatemodule() {
		var node = $("#moduleTree").tree("getSelected");
		if (node == null) {
			$.messager.alert("警告信息", "请选择要修改的模块！", "info");
		}
		$.ajax({
			type : "POST",
			dataType : "json",
			url : "module.do?method=toeditModule&nowdate=" + new Date(),
			data : {
				"moduleId" : node.id
			},
			success : function(data) {
				$("#moduleId").attr("value", data.moduleId);
				$("#modulePid").attr("value", data.modulePid);
				$("#moduleName").attr("value", data.moduleName);
				$("#moduleUrl").attr("value", data.moduleUrl);
				$("#modulePxh").attr("value", data.modulePxh);
			}
		});
		$("#addmodule").show();
		$("#addmodule").dialog({
			title : "更新模块",
			modal : true,
			width : 280,
			height : 200,
			collapsible : true,
			minimizable : false,
			maximizable : false,
			buttons : [ {
				text : "提交",
				iconCls : "icon-ok",
				handler : function() {
					$("#moduleform").form("submit", {
						url : "module.do?method=updateModule",
						onSubmit : function() {
							return $(this).form("validate");
						},
						success : function(data) {
							$("#addmodule").dialog("close");
							$.messager.show({
								title : "提示信息",
								msg : data,
								showType : "show",
								timeout : 3000
							});
							reload();
						}
					});
				}
			}, {
				text : "取消",
				handler : function() {
					$("#addmodule").dialog("close");
				}
			} ]
		});
	}
	//更新模块
	function deletemodule() {
		var node = $("#moduleTree").tree("getSelected");
		if (node == null) {
			$.messager.alert("警告信息", "请选择要修改的模块！", "info");
		}
		$.ajax({
			type : "POST",
			dataType : "text",
			url : "module.do?method=deleteModule&nowdate=" + new Date(),
			data : {
				"moduleId" : node.id
			},
			success : function(data) {
				$.messager.show({
					title : "提示信息",
					msg : data,
					showType : "show",
					timeout : 3000
				});
				reload();
			}
		});
	}
</script>
	</head>
	<body class="easyui-layout" style="overflow-y: hidden" scroll="no">
		<div region="west" style="width: 180px;" id="west">
			<div style="background-color: #E0EC00;margin-top: 1px;text-align: center;" >
			<a href="javascript:void(0)"  onclick="addmodule()" title="添加模块~" class="vtip">添加</a>|
			<a href="javascript:void(0)"  onclick="updatemodule()" title="修改模块~" class="vtip">修改</a>|
			<a href="javascript:void(0)"  onclick="deletemodule()" title="删除模块~" class="vtip">删除</a>|
			<a href="javascript:void(0)"  onclick="reload()" title="刷新模块树~" class="vtip">刷新</a>
			</div>
			<ul id="moduleTree" style="margin-top: 3px"></ul>
		</div>
		<div id="mainPanle" region="center" style="overflow: hidden">
			<table id="moduletab"></table>
		</div>
		<div id="addmodule"  style="display: none;text-align: center;">
			<form action="" name="moduleform"
				id="moduleform" method="post">
				<input type="hidden" name="modulePid" id="modulePid">
				<input type="hidden" name="moduleId" id="moduleId">
				<table>
					<tr>
						<td>
							模块名称：
						</td>
						<td>
							<input type="text" name="moduleName" id="moduleName"
								class="easyui-validatebox" required="true">
						</td>
					</tr>
					<tr>
						<td>
							排序号：
						</td>
						<td>
							<input type="text" name="modulePxh" id="modulePxh"
							class="easyui-numberbox">
						</td>
					</tr>
					<tr>
						<td>
							访问路径：
						</td>
						<td>
							<input type="text" name="moduleUrl" id="moduleUrl"
								class="easyui-validatebox" required="true">
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div id="selectrole" style="display: none;text-align: center;overflow: hidden;">
			<table id="roletomodule" style="width: 565px;height: 328px;overflow: hidden;"></table>
		</div>
	</body>
</html>